/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.UserBean;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.StafferDao;
import com.minierp.dao.UserDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Staffer;
import com.minierp.model.User;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class UserFormCtr extends SimpleFormController {
	private UserDao userDao;
	private StafferDao stafferDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		UserBean bean = new UserBean();
		new User();
		User form;
		if (id != null && !id.equals("")) {
			form = (User) this.userDao.get(User.class, Long.parseLong(id));
			if (form != null && form.getStafferId() != 0L) {
				Staffer sf = (Staffer) this.stafferDao.get(Staffer.class,
						form.getStafferId());
				if (sf != null) {
					bean.setStafferName(sf.getStafferName());
				}
			}

			if (form != null && form.getValidity() != null) {
				SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
				bean.setValidityStr(sf1.format(form.getValidity()));
			}
		} else {
			form = new User();
		}

		bean.setUser(form);
		return bean;
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String id = request.getParameter("id");
		User obj = ((UserBean) command).getUser();
		String alertMsg = "";
		if (id != null && !id.equals("")) {
			this.userDao.update(obj);
			alertMsg = "修改成功！";
		} else {
			obj.setId(this.idGenerator.getId(User.class.getName()));
			this.userDao.save(obj);
			alertMsg = "增加成功！";
		}

		String continueFlag = request.getParameter("continue_flag");
		ModelAndView mv;
		if (continueFlag != null && continueFlag.equals("1")) {
			mv = new ModelAndView("redirect:/system/UserNew.ihtm");
		} else {
			mv = new ModelAndView(this.getSuccessView());
			mv.addObject("returnValue", String.valueOf(obj.getId()) + ";"
					+ ((UserBean) command).getStafferName());
		}

		EncCommonHelper.setAlertMessage(request, alertMsg);
		return mv;
	}

	public UserDao getUserDao() {
		return this.userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public StafferDao getStafferDao() {
		return this.stafferDao;
	}

	public void setStafferDao(StafferDao stafferDao) {
		this.stafferDao = stafferDao;
	}
}